package com.dianping.cat.consumer.storage.model.transform;

import com.dianping.cat.consumer.storage.model.IVisitor;
import com.dianping.cat.consumer.storage.model.entity.Domain;
import com.dianping.cat.consumer.storage.model.entity.Machine;
import com.dianping.cat.consumer.storage.model.entity.Operation;
import com.dianping.cat.consumer.storage.model.entity.Segment;
import com.dianping.cat.consumer.storage.model.entity.Sql;
import com.dianping.cat.consumer.storage.model.entity.StorageReport;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

/*  23 */   public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
/*  27 */     this.m_out = new DataOutputStream(out);
/*  28 */     this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(StorageReport storageReport) {
    ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

/*  34 */     build(storageReport, out);
/*  35 */     return out.toByteArray();
  }


/*  39 */   public static void build(StorageReport storageReport, OutputStream out) { storageReport.accept(new DefaultNativeBuilder(out)); }



  public void visitDomain(Domain domain) {
/*  44 */     if (domain.getId() != null) {
/*  45 */       writeTag(1, 1);
/*  46 */       writeString(domain.getId());
    }

/*  49 */     if (!domain.getOperations().isEmpty()) {
/*  50 */       writeTag(33, 2);
/*  51 */       writeInt(domain.getOperations().size());

/*  53 */       for (Operation operation : domain.getOperations().values()) {
/*  54 */         operation.accept(this.m_visitor);
      }
    }

/*  58 */     if (!domain.getSqls().isEmpty()) {
/*  59 */       writeTag(34, 2);
/*  60 */       writeInt(domain.getSqls().size());

/*  62 */       for (Sql sql : domain.getSqls().values()) {
/*  63 */         sql.accept(this.m_visitor);
      }
    }

/*  67 */     writeTag(63, 3);
  }


  public void visitMachine(Machine machine) {
/*  72 */     if (machine.getId() != null) {
/*  73 */       writeTag(1, 1);
/*  74 */       writeString(machine.getId());
    }

/*  77 */     if (!machine.getDomains().isEmpty()) {
      writeTag(33, 2);
/*  79 */       writeInt(machine.getDomains().size());

/*  81 */       for (Domain domain : machine.getDomains().values()) {
/*  82 */         domain.accept(this.m_visitor);
      }
    }

/*  86 */     writeTag(63, 3);
  }


  public void visitOperation(Operation operation) {
/*  91 */     if (operation.getId() != null) {
/*  92 */       writeTag(1, 1);
/*  93 */       writeString(operation.getId());
    }

/*  96 */     writeTag(2, 0);
/*  97 */     writeLong(operation.getCount());

/*  99 */     writeTag(3, 0);
/* 100 */     writeDouble(operation.getAvg());

/* 102 */     writeTag(4, 0);
/* 103 */     writeDouble(operation.getSum());

/* 105 */     writeTag(5, 0);
/* 106 */     writeLong(operation.getError());

/* 108 */     writeTag(6, 0);
/* 109 */     writeLong(operation.getLongCount());

/* 111 */     if (!operation.getSegments().isEmpty()) {
/* 112 */       writeTag(33, 2);
/* 113 */       writeInt(operation.getSegments().size());

/* 115 */       for (Segment segment : operation.getSegments().values()) {
/* 116 */         segment.accept(this.m_visitor);
      }
    }

/* 120 */     writeTag(63, 3);
  }


  public void visitSegment(Segment segment) {
/* 125 */     writeTag(1, 0);
/* 126 */     writeInt(segment.getId());

/* 128 */     writeTag(2, 0);
/* 129 */     writeLong(segment.getCount());

/* 131 */     writeTag(3, 0);
/* 132 */     writeDouble(segment.getAvg());

/* 134 */     writeTag(4, 0);
/* 135 */     writeDouble(segment.getSum());

/* 137 */     writeTag(5, 0);
/* 138 */     writeLong(segment.getError());

/* 140 */     writeTag(6, 0);
/* 141 */     writeLong(segment.getLongCount());

/* 143 */     writeTag(63, 3);
  }


  public void visitSql(Sql sql) {
/* 148 */     if (sql.getId() != null) {
/* 149 */       writeTag(1, 1);
/* 150 */       writeString(sql.getId());
    }

/* 153 */     if (sql.getStatement() != null) {
/* 154 */       writeTag(2, 1);
/* 155 */       writeString(sql.getStatement());
    }

/* 158 */     writeTag(3, 0);
/* 159 */     writeInt(sql.getCount());

/* 161 */     writeTag(63, 3);
  }


  public void visitStorageReport(StorageReport storageReport) {
/* 166 */     writeTag(63, 0);

/* 168 */     if (storageReport.getId() != null) {
/* 169 */       writeTag(1, 1);
/* 170 */       writeString(storageReport.getId());
    }

/* 173 */     if (storageReport.getName() != null) {
/* 174 */       writeTag(2, 1);
/* 175 */       writeString(storageReport.getName());
    }

/* 178 */     if (storageReport.getType() != null) {
/* 179 */       writeTag(3, 1);
/* 180 */       writeString(storageReport.getType());
    }

/* 183 */     if (storageReport.getStartTime() != null) {
/* 184 */       writeTag(4, 1);
/* 185 */       writeDate(storageReport.getStartTime());
    }

/* 188 */     if (storageReport.getEndTime() != null) {
/* 189 */       writeTag(5, 1);
/* 190 */       writeDate(storageReport.getEndTime());
    }

/* 193 */     if (storageReport.getIds() != null) {
/* 194 */       writeTag(6, 2);
/* 195 */       writeInt(storageReport.getIds().size());

/* 197 */       for (String id : storageReport.getIds()) {
/* 198 */         writeString(id);
      }
    }

/* 202 */     if (storageReport.getIps() != null) {
/* 203 */       writeTag(7, 2);
/* 204 */       writeInt(storageReport.getIps().size());

/* 206 */       for (String ip : storageReport.getIps()) {
/* 207 */         writeString(ip);
      }
    }

/* 211 */     if (storageReport.getOps() != null) {
/* 212 */       writeTag(8, 2);
/* 213 */       writeInt(storageReport.getOps().size());

/* 215 */       for (String op : storageReport.getOps()) {
/* 216 */         writeString(op);
      }
    }

/* 220 */     if (!storageReport.getMachines().isEmpty()) {
/* 221 */       writeTag(33, 2);
/* 222 */       writeInt(storageReport.getMachines().size());

/* 224 */       for (Machine machine : storageReport.getMachines().values()) {
/* 225 */         machine.accept(this.m_visitor);
      }
    }

/* 229 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/* 234 */       writeVarint(value.getTime());
/* 235 */     } catch (Exception e) {
/* 236 */       throw new RuntimeException(e);
    }
  }

  private void writeDouble(double value) {
    try {
/* 242 */       writeVarint(Double.doubleToLongBits(value));
/* 243 */     } catch (Exception e) {
/* 244 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 250 */       writeVarint(value & 0xFFFFFFFFL);
/* 251 */     } catch (Exception e) {
/* 252 */       throw new RuntimeException(e);
    }
  }

  private void writeLong(long value) {
    try {
/* 258 */       writeVarint(value);
/* 259 */     } catch (Exception e) {
/* 260 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 266 */       this.m_out.writeUTF(value);
/* 267 */     } catch (IOException e) {
/* 268 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 274 */       this.m_out.writeByte((field << 2) + type);
/* 275 */     } catch (IOException e) {
/* 276 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 282 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 283 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 286 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 287 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/storage/model/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */